package com.longrise.filedownloaderdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.longrise.filedownloader.DownloadFileInfo;
import com.longrise.filedownloader.DownloadManager;
import com.longrise.filedownloader.DownloadService;
import com.longrise.filedownloader.OnFileDownloadStatusListener;

import java.util.ArrayList;


/**
 * Created by xkj on 16/4/15.
 *
 */
public class DownloadDetailActivity extends Activity implements View.OnClickListener, UpdateCallBack, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private static final String TAG = "DownloadDetailActivity";
    private RelativeLayout mRlback;
    private DownloadedDetailAdapter mAdapter;
    private ListView mLv;
    private ArrayList<String> mAllTaskUrl;
    private DownloadManager mDownloadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloaddetail);
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        mRlback = (RelativeLayout) findViewById(R.id.downloaddetail_relapp_back);
        mLv = (ListView) findViewById(R.id.downloaddetail_lv);
    }

    private void initData(){
        mDownloadManager = DownloadService.getDownloadManager();
        //mDownloadManager = DownloadManager.getInstance(DownloadDetailActivity.this, "hehe");
        mAdapter = new DownloadedDetailAdapter(DownloadDetailActivity.this, mDownloadManager);

        mAllTaskUrl = mDownloadManager.getAllTaskUrl();
        if(mAllTaskUrl !=null){
            mAdapter.setData(mAllTaskUrl);
        }
        mAdapter.setUpdateCallback(this);
        mLv.setAdapter(mAdapter);
    }

    private void initEvent(){
        mRlback.setOnClickListener(this);
        mLv.setOnItemClickListener(this);
        mLv.setOnItemLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.downloaddetail_relapp_back :
                finish();
                break;
        }
    }

    @Override
    public void startProgress(String url, final int position) {
        Log.d(TAG,"URL:"+url+";;;"+position);
        mDownloadManager.setOnFileDownloadStatusListener(url, new OnFileDownloadStatusListener() {
            @Override
            public void onFileDownloadStatusWaiting(DownloadFileInfo downloadFileInfo) {
                updateProgressPartly(1,downloadFileInfo,position);
            }

            @Override
            public void onFileDownloadStatusDownloading(DownloadFileInfo downloadFileInfo) {
                Log.d(TAG,"downloadFileInfo:"+downloadFileInfo.getDownloadSize());
                updateProgressPartly(2,downloadFileInfo,position);

            }

            @Override
            public void onFileDownloadStatusPaused(DownloadFileInfo downloadFileInfo) {
                updateProgressPartly(3,downloadFileInfo,position);
            }

            @Override
            public void onFileDownloadStatusCompleted(DownloadFileInfo downloadFileInfo) {
                updateProgressPartly(4,downloadFileInfo,position);
            }

            @Override
            public void onFileDownloadStatusFailed(DownloadFileInfo downloadFileInfo) {
                updateProgressPartly(5,downloadFileInfo,position);
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d(TAG,"click:"+position);
        String url = mAllTaskUrl.get(position);
        mDownloadManager.switchStatus(url);

    }

    private void updateProgressPartly(int mark , DownloadFileInfo downloadFileInfo,int position) {
        int firstVisiblePosition = mLv.getFirstVisiblePosition();
        int lastVisiblePosition = mLv.getLastVisiblePosition();
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            View view = mLv.getChildAt(position - firstVisiblePosition);
            if (view.getTag() instanceof DownloadedDetailAdapter.Holder) {
                DownloadedDetailAdapter.Holder viewHolder = (DownloadedDetailAdapter.Holder) view.getTag();
                long downloadSize = downloadFileInfo.getDownloadSize();
                long fileSize = downloadFileInfo.getFileSize();
                int progress = downloadFileInfo.getProgress();

                String fileDownloadedSize =FileUtil.FormetFileSize(downloadSize);
                String fileSize2 = FileUtil.FormetFileSize(fileSize);
                viewHolder.mPb.setProgress(progress);
                viewHolder.mTvpb.setText(fileDownloadedSize+"/"+fileSize2);

                switch (mark){

                    case 1:
                        viewHolder.mTvstatus1.setText("等待中");
                        break;
                    case 2:
                        viewHolder.mTvstatus1.setText("下载中");
                        break;
                    case 3:
                        viewHolder.mTvstatus1.setText("暂停中");
                        break;
                    case 4:
                        viewHolder.mTvstatus1.setText("下载成功");
                        break;
                    case 5:
                        viewHolder.mTvstatus1.setText("下载失败");
                        break;
                }

            }
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String url = mAllTaskUrl.get(position);
        boolean b = mDownloadManager.deleteTask(url);
        if(b){
            mAllTaskUrl.clear();
            ArrayList<String> allTaskUrl = mDownloadManager.getAllTaskUrl();
            mAllTaskUrl.addAll(allTaskUrl);
            mAdapter.setData(mAllTaskUrl);
        }

        return true;
    }
}
